<template>
  <div>
    <Modal v-model="show" @on-cancel="cancel" :mask-closable="false">
      <Form ref="formInline" :model="formInline" :rules="ruleInline">
        <FormItem label="用户名：" prop="user" style="display:inline">
          <Input type="text" inline v-model="formInline.user" placeholder="请输入用户名">
            <Icon type="ios-person-outline" slot="prepend"></Icon>
          </Input>
        </FormItem>
        <FormItem label="密码：" style="display:inline" prop="password">
          <Input type="password" v-model="formInline.password" placeholder="请输入密码">
            <Icon type="ios-lock-outline" slot="prepend"></Icon>
          </Input>
        </FormItem>
        <FormItem label="14天内免登录：" style="display:inline">
          <i-switch v-model="remember" size="large"></i-switch>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" size="large" @click="cancel">取消</Button>
        <Button type="primary" size="large" :loading="loading" @click="ok">登陆</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import crypto from "crypto";
export default {
  name: "LoginModal",
  props: ["show_parent"],
  data() {
    return {
      show: this.show_parent,
      loading: false,
      remember: true,
      formInline: {
        user: "",
        password: ""
      },
      ruleInline: {
        user: [
          { required: true, message: "请输入用户名", trigger: "change,blur" },
          {
            validator: (rule, value, callback) => {
              var reg = new RegExp("^[a-zA-Z0-9_]{5,18}$");
              if (!reg.test(value)) {
                callback(true);
              } else {
                callback();
              }
            },
            message: "用户名只能包括数字、字母、下划线，且长度必须在5-18位",
            trigger: "change,blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change,blur" },
          {
            validator: (rule, value, callback) => {
              var reg = new RegExp(
                "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z\\W]{6,18}$"
              );
              if (!reg.test(value)) {
                callback(true);
              } else {
                callback();
              }
            },
            message: "密码必须为6-18位，只能包括数字、字母、特殊字符，且不能为纯字母、数字",
            trigger: "change,blur"
          }
        ]
      }
    };
  },
  methods: {
    ok() {
      this.$refs["formInline"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$api.post(
            "login/",
            {
              username: this.formInline.user,
              password: this.setMd5(this.formInline.password),
              remember: this.remember
            },
            response => {
              var params = { logined: false };
              if (response.status != 200) {
                this.loading = false;
                this.$Message.error("登陆失败，服务器错误");
                this.$Message.error("" + response);
                console.log(response);
              } else {
                var data = response.data;
                if(data.err_code != 0)
                {
                  this.$Message.error("登陆失败"+data.error);
                  this.loading = false;
                  this.$Message.error(data.error);
                }
                else{
                  data=data.data;
                  if(data.result==true){
                  this.$Message.success("登陆成功");
                  this.show = false;
                  params.logined = true;
                  params.data = data;
                  this.$emit("loginClosed", params);
                  }
                  else{
                    this.loading = false;
                    this.$Message.error(data.error);
                  }
                }
              }
            }
          );
        } else {
          this.$Message.error("请更正信息后登陆");
          this.$refs.modal.buttonLoading = false;
        }
      });
    },
    cancel() {
      var params = { logined: false };
      this.show = false;
      this.$emit("loginClosed", params);
    },
    setMd5(text) {
      var md5 = crypto.createHash("md5");
      md5.update(text);
      var text_md5 = md5.digest("hex");
      return text_md5;
    }
  }
};
</script>
<style scoped>
</style>
